home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / abbrev4 / abbrev.cb next >
Text File  |  1989-11-16  |  10KB  |  233 lines

  1. /*************************************************************************
  2. *                                                                                                *
  3. *     If you are a previous user of ABBREV, you will need to                     *
  4. *     replace the autoload's that were provided with that version                 *
  5. *     with the ones specified below.                                                     *
  6. *                                                                                                *
  7. **************************************************************************
  8. *                                                                                                *
  9. *      ABBREV.CB                                                                                 *
  10. *                                                                                                  *
  11. *      This macro provides a replacement for edit_file and read_file.  It     *
  12. *      allows arbitrary abbreviation of drive and path combinations in         *
  13. *      file names (and may even be used to abbreviate full file names).         *
  14. *                                                                                                  *
  15. *      The if there is a slash or backslash in the path (after column 1),     *
  16. *      then abbrev looks up a replacement for what preceeds the                 *
  17. *      1st slash (backslash) and passes it on to the next                         *
  18. *      program in the chain.     If there is no slash or backslash,                 *
  19. *      abbrev checks for a replacement of the entire name                         *
  20. *      specified.  (This is for using an abbreviation for a                         *
  21. *      file name.)  It is not necessary to use the last                             *
  22. *      directory name as the abbreviation, and you may                             *
  23. *      supply several abbreviations for a single directory.                         *
  24. *                                                                                                  *
  25. *      Finally, if you wish to pass a name through without being                 *
  26. *      altered by abbrev, then precede it with an exclaimation point.         *
  27. *                                                                                                  *
  28. *      If "foo" was the abbreviation for "c:\new\dos\foo"                         *
  29. *                                                                                                  *
  30. *      then (edit_file "foo\filename") would pass on:                                 *
  31. *                                                                                                  *
  32. *              (edit_file "c:\new\dos\foo\filename").                                 *
  33. *                                                                                                  *
  34. *      The abbreviations are kept in a file called "abbrev.dat".                 *
  35. *      This file goes in the last directory specified by your                     *
  36. *      BPATH environment variable.    If you would like it in a                     *
  37. *      different directory, then you may specify this with the                     *
  38. *      BABBREV environment variable.  The macro "abbrev" (invoked with         *
  39. *      F10) will bring the abbrev.dat file into a Brief buffer to be             *
  40. *      edited.                                                                                     *
  41. *                                                                                                  *
  42. *      Each abbreviation is specified on 1 line of the file.                     *
  43. *      The line is of the form:                                                             *
  44. *                                                                                                  *
  45. *      <abbreviation> <whitespace> <substitution>                                     *
  46. *                                                                                                  *
  47. *      The <abbreviation> must begin in column 1.    The line                         *
  48. *      for the above example would be:                                                     *
  49. *                                                                                                  *
  50. *      foo         c:\new\dos\foo                                                             *
  51. *                                                                                                  *
  52. *      To inspire you of its usefulness, part of my current file is             *
  53. *      setup as follows:                                                                     *
  54. *                                                                                                  *
  55. *      autoexec c:\autoexec.bat                                                             *
  56. *      exec      c:\autoexec.bat                                                             *
  57. *      config     c:\config.sys                                                                 *
  58. *      ap         d:\pinball\apple                                                             *
  59. *      apple     d:\pinball\apple                                                             *
  60. *      util      d:\util                                                                         *
  61. *      u          d:\util                                                                         *
  62. *      uni         d:\util\uni                                                                 *
  63. *      mine      c:\soft\brief\macros\mine                                                 *
  64. *      m          c:\soft\brief\macros\mine                                                 *
  65. *      macros     c:\soft\brief\macros                                                     *
  66. *      mac         c:\soft\brief\macros                                                     *
  67. *      macs      c:\soft\brief\macros                                                     *
  68. *      inc         c:\soft\c\include                                                         *
  69. *      include  c:\soft\c\include                                                         *
  70. *                                                                                                  *
  71. *      To install, add the following to your initials macro                         *
  72. *      (after any other replacements to edit_file or read_file):                 *
  73. *                                                                                                  *
  74. *   autoload ("abbrev.cm", "edit_file", "read_file", "abbrev")                  *
  75. *      autoload ("ablookup.cm", "ab_lookup", "get_abbrev_file")                 *
  76. *                                                                                                  *
  77. *      Place "abbrev.cm" and "ablookup.cm" in a BPATH directory                 *
  78. *      (e.g. c:\brief\macros).                                                             *
  79. *                                                                                                  *
  80. *      Abbrev keeps the abbreviation file in a system buffer.                     *
  81. *      The 1st call will create the system buffer.   Subsequent calls         *
  82. *      will run faster.                                                                         *
  83. *                                                                                                  *
  84. *      Larry DeMar     July, 1989                                                             *
  85. *                                                                                                  *
  86. *      Updated for FILE_ED3 compatability,  August, 1989.   -Led                 *
  87. *                                                                                                  *
  88. *      Updated to allow suffix.cb to use the utilities without loading         *
  89. *      replacement macros October, 1989  -Led.                                         *
  90. *                                                                                                *
  91. *************************************************************************/
  92.  
  93. #define ABBREV_FILE_NAME "abbrev.dat"
  94. #define TRUE 1
  95. #define FALSE 0
  96.  
  97. extern ab_lookup(...);
  98. extern get_abbrev_file(...);
  99.  
  100. replacement edit_file (...)
  101. {
  102.     string e_parm;
  103.  
  104.     if (inq_called () != "")
  105.         return edit_file ();
  106.     if (!get_parm (0, e_parm, "File: "))
  107.         return;
  108.     edit_file (abbreviate (e_parm));
  109. }
  110.  
  111. replacement read_file (...)
  112. {
  113.     string r_parm;
  114.  
  115.     if (inq_called () != "")
  116.         return read_file ();
  117.     if (!get_parm (0, r_parm, "File to read: "))
  118.         return;
  119.     read_file (abbreviate (r_parm));
  120. }
  121.  
  122. /*************************************************************************
  123. *                                                                        *
  124. *      ABBREVIATE                                                                                 *
  125. *                                                                                                  *
  126. *      This is called to expand an abbreviation if necessary.                     *
  127. *                                                                                                  *
  128. *      It returns the string that should be passed on.                             *
  129. *                                                                        *
  130. *************************************************************************/
  131. abbreviate (...)
  132. {
  133.     int slash_index;
  134.     global int abbrev_buffer_id;
  135.     string abbreviation,
  136.              file_name,
  137.              first_char;          // allow leading slash or backslash as a courtesy 
  138.  
  139.     get_parm (0, file_name);
  140.     if (substr (file_name, 1, 1) == "!")     // check for bang                     
  141.         return substr (file_name, 2);          // strip it and we're done.         
  142.     //                                                                                              
  143.     //   As a courtesy, if the user preceeded his abbreviation                     
  144.     //   with slash or backslash (from using dos too much)                         
  145.     //   lookup what follows the leading seperator                                     
  146.     //                                                                                              
  147.     //   If you want to use that leading slash or backslash                         
  148.     //   to shut off abbrev, then comment out the next 2                             
  149.     //   Brief statements (next 6 lines).                                                 
  150.     //                                                                                              
  151.     first_char = substr (file_name, 1, 1);               // get 1st char          
  152.     if (first_char == "/" || first_char == "\\")
  153.         file_name = substr (file_name, 2);             // lookup rest                 
  154.     else
  155.         first_char = "";                                    // nothing stripped             
  156.     //                                                                                              
  157.     //   now we have to see if there is a path separator.                          
  158.     //                                                                                              
  159.     slash_index = get_first_seperator (file_name);
  160.  
  161.     //       slash_index is now zero if there are now separators                  
  162.     //       and has the position of the 1st seperator if non-zero.              
  163.     //                                                                                              
  164.     if (slash_index)
  165.         abbreviation = substr (file_name, 1, slash_index - 1);
  166.     else
  167.         abbreviation = file_name;
  168.  
  169.     if (index (abbreviation, "*") || index (abbreviation, "?"))     // for FILE_ED3 if it has a star         
  170.         abbreviation = first_char + abbreviation;      // don't look up!         
  171.  
  172.     else
  173.         if (!ab_lookup (ABBREV_FILE_NAME, abbreviation, abbrev_buffer_id, "abbrev.$b$"))
  174.             abbreviation = first_char + abbreviation;      // not found..put 1st char back. 
  175.     //                                                                                              
  176.     //   now....if there was a path...we must concatonate whats beyond 1st seperator 
  177.     //                                                                                              
  178.     if (slash_index)
  179.         abbreviation = abbreviation + substr (file_name, slash_index);
  180.  
  181.     return abbreviation;
  182. }
  183.  
  184. /*************************************************************************
  185. *                                                                        *
  186. *      ABBREV                                                                                     *
  187. *                                                                                                  *
  188. *      This is called to bring the abbreviation file into the                     *
  189. *      editor.  It first deletes the system buffer such that                     *
  190. *      changes (that are written) will be used on the next                         *
  191. *      request.                                                                                 *
  192. *                                                                        *
  193. *************************************************************************/
  194. abbrev (...)
  195. {
  196.     if (abbrev_buffer_id)
  197.         delete_buffer (abbrev_buffer_id);
  198.     abbrev_buffer_id = 0;
  199.     edit_file (get_abbrev_file (ABBREV_FILE_NAME));
  200. }
  201.  
  202. /*************************************************************************
  203. *                                                                        *
  204. *      GET_FIRST_SEPERATOR                                                                     *
  205. *                                                                                                  *
  206. *      This is called to return the position of the 1st seperator                 *
  207. *      (slash or backslash) in a file name.    It returns 0 if                     *
  208. *      no seperators exist, or the index of the 1st one it                         *
  209. *      finds.                                                                                     *
  210. *                                                                        *
  211. *************************************************************************/
  212. get_first_seperator (...)
  213. {
  214.     int slash_index,
  215.          bslash_index;
  216.  
  217.     string file_name;
  218.  
  219.     get_parm (0, file_name);
  220.  
  221.     slash_index = index (file_name, "/");
  222.     bslash_index = index (file_name, "\\");
  223.     if (slash_index && bslash_index)       // if both seperators are there     
  224.         if (bslash_index < slash_index)
  225.             slash_index = bslash_index;    // then take the lower                     
  226.         else ;
  227.     else
  228.         if (                        // is 1st zero?                                             
  229.             bslash_index)
  230.             slash_index = bslash_index;    // yep...use 2nd.                          
  231.     return slash_index;
  232. }
  233.